package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.ProcessTypeListDTO;
import com.xbongbong.paas.pojo.vo.ProcessTypeListVO;
import com.xbongbong.paas.service.PaasProcessListSearchService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.process.pojo.dto.ProcessAppFilterDTO;
import com.xbongbong.pro.process.pojo.dto.ProcessFormFilterDTO;
import com.xbongbong.pro.process.pojo.vo.ProcessAppFilterVO;
import com.xbongbong.pro.process.pojo.vo.ProcessFormFilterVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Description: 流程列表筛选项
 * @author 李杰
 * @date 2019/1/22 10:16
 * @version v1.0
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/process/search")
public class MobilePaasProcessListSearchController {

    @Resource
    private PaasProcessListSearchService paasProcessListSearchService;

    /**
     * app筛选项
     * @param processAppFilterDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/appFilter", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String appFilter(@RequestBody @Valid ProcessAppFilterDTO processAppFilterDTO, BindingResult br) throws Exception {
        XbbResponse<ProcessAppFilterVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessAppFilterVO processAppFilterVO = paasProcessListSearchService.appFilter(processAppFilterDTO);
                response = new XbbResponse<>(processAppFilterVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 表单筛选项
     * @param processFormFilterDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/formFilter", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formFilter(@RequestBody @Valid ProcessFormFilterDTO processFormFilterDTO, BindingResult br) throws Exception {
        XbbResponse<ProcessFormFilterVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessFormFilterVO processFormFilterVO = paasProcessListSearchService.formFilter(processFormFilterDTO);
                response = new XbbResponse<>(processFormFilterVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 流程状态筛选项
     * @param processTypeListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/typeFilter", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String typeList(@RequestBody @Valid ProcessTypeListDTO processTypeListDTO, BindingResult br) {
        XbbResponse<ProcessTypeListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessTypeListVO processTypeListVO = paasProcessListSearchService.getProcessTypeList(processTypeListDTO);
                response = new XbbResponse<>(processTypeListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
